﻿using System;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Reflection;
using MySql.Data.MySqlClient;

namespace YJKDataBase
{
	public class MySqlObjectFileTable : IObjectFileTable
	{
		public MySqlObjectFileTable(MySqlDatabase db)
		{
			this.mDatabase = db;
		}

		public void Update()
		{
		}

		public bool Add(DbObject dbObject, FileInfo fi, DbTransaction tr)
		{
			byte[] array = Assist.ReadFile(fi.FullName);
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			bool result;
			try
			{
				using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
				{
					mySqlCommand.Transaction = (MySqlTransaction)tr;
					mySqlCommand.CommandText = string.Format("insert into ObjectFileTable (ObjectGUID,ObjectFile) values ('{0}',{1})", dbObject.Guid.ToString(), "?file");
					MySqlParameter mySqlParameter = new MySqlParameter("?file", MySqlDbType.Blob, array.Length);
					mySqlParameter.Value = array;
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
				}
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool Remove(DbObject dbObject, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from ObjectFileTable where ObjectGUID='" + dbObject.Guid.ToString() + "'", tr);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public FileInfo this[DbObject dbObject, string exeName]
		{
			get
			{
				string text = Assembly.GetExecutingAssembly().Location;
				int num = text.LastIndexOf("\\");
				text = text.Substring(0, num + 1);
				FileInfo result = null;
				if (this.mDatabase.dbCon.State == ConnectionState.Closed)
				{
					this.mDatabase.dbCon.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery("select ObjectFile from ObjectFileTable where ObjectGUID='" + dbObject.Guid.ToString() + "'");
				DataTable dataTable2 = this.mDatabase.ExecQuery("select Name from ObjectTable where GUID='" + dbObject.Guid.ToString() + "'");
				if (this.mDatabase.dbCon.State == ConnectionState.Open)
				{
					this.mDatabase.dbCon.Close();
				}
				if (dataTable != null && dataTable2 != null)
				{
					byte[] pReadByte = (byte[])dataTable.Rows[0][0];
					string fileName = string.Concat(new string[]
					{
						text,
						"\\",
						dataTable2.Rows[0][0].ToString(),
						".",
						exeName
					});
					Assist.WriteFile(pReadByte, fileName);
					result = new FileInfo(fileName);
				}
				return result;
			}
		}

		public bool Replace(DbObject dbObject, FileInfo fi)
		{
			DbTransaction dbTransaction = this.mDatabase.StartTransaction();
			byte[] array = Assist.ReadFile(fi.FullName);
			MySqlConnection mySqlConnection = (MySqlConnection)this.mDatabase.dbCon;
			bool result;
			try
			{
				if (mySqlConnection.State == ConnectionState.Closed)
				{
					mySqlConnection.Open();
				}
				using (MySqlCommand mySqlCommand = mySqlConnection.CreateCommand())
				{
					mySqlCommand.Transaction = (MySqlTransaction)dbTransaction;
					mySqlCommand.CommandText = string.Format("update ObjectFileTable set ObjectFile={0} where ObjectGUID='{1}'", "?file", dbObject.Guid.ToString());
					MySqlParameter mySqlParameter = new MySqlParameter("?file", MySqlDbType.Blob, array.Length);
					mySqlParameter.Value = array;
					mySqlCommand.Parameters.Add(mySqlParameter);
					mySqlCommand.ExecuteNonQuery();
				}
				this.mDatabase.CommitTransaction(dbTransaction);
				if (mySqlConnection.State == ConnectionState.Open)
				{
					mySqlConnection.Close();
				}
				result = true;
			}
			catch (Exception ex)
			{
				if (mySqlConnection.State == ConnectionState.Open)
				{
					mySqlConnection.Close();
				}
				throw ex;
			}
			return result;
		}

		private MySqlDatabase mDatabase;
	}
}
